package jp.dodododo.aop.interceptors;

import jp.dodododo.aop.annotation.Enhance;

import org.aopalliance.intercept.MethodInvocation;

/**
 * {@.en }
 *
 * <br />
 *
 * {@.ja 非同期でメソッドを実行するInterceptorです。}
 *
 * @author Satoshi Kimura
 */
@Enhance(false)
public class AsyncInterceptor extends AbstractMethodInterceptor {

	private static final AsyncInterceptor INSTANCE = new AsyncInterceptor();

	/**
	 *
	 * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
	 */
	public final Object invoke(final MethodInvocation invocation) throws Throwable {

		new Thread(new Runnable(invocation)).start();
		return null;
	}

	public static class Runnable implements java.lang.Runnable {

		private MethodInvocation invocation;

		public Runnable (MethodInvocation invocation) {
			this.invocation= invocation;
		}

		public void run() {
			try {
				invocation.proceed();
			} catch (Throwable t) {
				throw new RuntimeException(t);
			}
		}

	}

	protected static final AsyncInterceptor getInstance() {
		return INSTANCE;
	}

}
